package cz.uhk.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

import javax.swing.JTextField;

public class Stomo implements Serializable{
	
	float[][] subMatice;
	float[][] incidence;
	float[][] vysl, e, vektor;
	int mys, syr;
	
		
	public float vypocitejTeorStrDobuPruchodu(float[][] incidence, int mys, int syr, JTextField tf){
		this.incidence = incidence;
		this.mys = mys; this.syr = syr;
		
		
		ziskejSubmatici();
		ziskejJednotkovouMatici();
		ziskejVektor();
		
		vysl = subMatice;
		
		float f = 0;
		float[] poslednichDeset = {0,0,0,0,0,0,0,0,0,1};
		int i = 1;
		
		while(soucet(poslednichDeset) > 0.00001f){
			float p = f(i);
			float delta = p*(float)i;
			f += delta;
			tf.setText(f+"");
			i++;
			poslednichDeset[(i%10)] = delta;
		}
		return f;
	}
	
	private float soucet(float[] pole){
		float soucet = 0;
		for(int m = 0; m < pole.length; m++){
			soucet += pole[m];
		}
		return soucet;
	}
	
	public float f(int krok){
		float f = 0;
		vysl = subMatice;
		
		if(krok==1){
			f = multiply(e, vektor)[mys][0];
		}
		if(krok==2){
			f = multiply(subMatice, vektor)[mys][0];
		}
		else if(krok > 2){
			for(int i = 0; i < krok-2; i++){
				vysl = multiply(vysl, subMatice);
			}
			f= multiply(vysl, vektor)[mys][0];
		}
		
		return f;
	}
	
	
	private void ziskejSubmatici(){
		subMatice = new float[incidence.length-1][incidence.length-1];
		float[][] pom = new float[incidence.length][incidence.length];
		ArrayUtils.copyFloatArrays(incidence, pom);
		
		for(int i = 0; i < pom.length; i++){
			for(int j = syr; j < pom.length-1; j++){
				
				pom[i][j] = pom[i][j+1];
				
			}
		}
		
		for(int i = syr; i < pom.length-1; i++){
			for(int j = 0; j < pom.length; j++){
				
				pom[i][j] = pom[i+1][j];
				
			}
		}
		
		for(int i = 0; i < subMatice.length; i++){
			for(int j = 0; j < subMatice.length; j++){
				
				subMatice[i][j] = pom[i][j];
				
			}
		}
	}
	
	public void ziskejJednotkovouMatici(){
		e = new float[subMatice[0].length][subMatice.length];
		
		for(int i = 0; i < e.length; i++){
			for(int j = 0; j < e[0].length; j++){
				if(i==j) e[i][j] = 1;
				else  e[i][j] = 0;
			}
		}
	}
	
	public void ziskejVektor(){
		vektor = new float[subMatice.length][subMatice.length];
		
		Stack<Float> stack = new Stack<Float>();
		
		for(int i = 0; i<incidence.length; i++){
			if(i!=syr)stack.push(incidence[i][syr]);
		}
		
		for(int i = vektor.length-1; i >= 0; i--){
			vektor[i][0] = stack.pop();
		}		
	}
	
	public float[][] multiply(float[][] m, float[][] m2){
		
				
		float[][] res = new float[m.length][m2[0].length];
		for(int i = 0; i < m.length; i++) {
		    for(int j = 0; j < m2[0].length; j++) {
		        float sou = 0;
		        for(int s = 0; s<m2.length; s++){
		            sou += (m[i][s]*m2[s][j]);
		            }
		        res[i][j] = sou;
		        }
		    }
		
		
		return res;
	
}
	
	private void tisk(float[][] tisk){
		String radek;
		for(int i = 0; i<tisk.length; i++){
			radek = "";
			for(int j = 0; j < tisk.length; j++){
				radek = radek + tisk[i][j]+" ";
			}
			System.out.println(radek);
		}
	}

}
